package at.fhv.androideightpuzzle.network.message;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class GameResultMessage implements IMessage{
	
	public static final int TYPE=0;
	
	private String[] m_playerNames;
	private long[] m_durations;
	
	public GameResultMessage(String[] playerNames,long[] durations){
		m_playerNames=playerNames;
		m_durations=durations;
	}
	
	@SuppressWarnings("unchecked")
	public String getJson(){
		JSONObject jsonObj=new JSONObject();
		
		jsonObj.put("type",GameResultMessage.TYPE);
		
		JSONObject payload=new JSONObject();
		
		JSONArray playerNamesJsonArr=new JSONArray();
		
		for(int i=0;i<m_playerNames.length; i++){
			playerNamesJsonArr.add(m_playerNames[i]);
		}
		
		JSONArray durationsJsonArray=new JSONArray();
		
		for(int i=0; i<m_durations.length; i++){
			durationsJsonArray.add(m_durations[i]);
		}
		
		payload.put("players",playerNamesJsonArr);
		payload.put("durations",durationsJsonArray);
		
		jsonObj.put("payload",payload);
		
		return jsonObj.toJSONString();
	}
	
	public String[] getPlayerNames(){
		return m_playerNames;
	}
	
	public long[] getDurations(){
		return m_durations;
	}
}
